import Vue from 'vue'
import { infoMessage } from '@/hooks'

/**
 * 确认消息对话框
 * @param {*} content 提示内容
 * @param sure
 * @param cancel
 * @author 赵京涛
 * @returns
 */
export function useSureMessageBox(content, sure, cancel) {
  return Vue.prototype.$confirm(content, '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    sure()
  }).catch(() => {
    cancel()
  })
}

/**
 * 验证邮箱
 * @author 赵京涛
 */
export function usePromptEmailMsgBox() {
  return Vue.prototype.$prompt('请输入邮箱', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:\w(?:[\w-]*\w)?\.)+\w(?:[\w-]*\w)?/,
    inputErrorMessage: '邮箱格式不正确'
  }).then(({ value }) => {
    Vue.prototype.$message({
      type: 'success',
      message: '你的邮箱是: ' + value
    })
  }).catch(() => {
    Vue.prototype.$message({
      type: 'info',
      message: '取消输入'
    })
  })
}

/**
 * 自定义提示信息
 * @param message
 * @param prompt
 * @param sureHandler
 * @author 赵京涛
 */
export function useCustomizePromptMsgBox(message, prompt, sureHandler) {
  return Vue.prototype.$msgbox({
    title: '提示',
    message,
    type: 'warning',
    showCancelButton: true,
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    beforeClose: (action, instance, done) => {
      if (action === 'confirm') {
        instance.confirmButtonLoading = true
        instance.confirmButtonText = `正在${prompt}中...`
        setTimeout(() => {
          done()
          setTimeout(() => {
            instance.confirmButtonLoading = false
          }, 300)
        }, 900)
      } else {
        infoMessage(`已取消${prompt}`)
        done()
      }
    }
  }).then(() => {
    sureHandler()
  })
}
